package com.t114514.demo.controller;

import com.t114514.demo.pojo.Answer;
import com.t114514.demo.pojo.Result;
import com.t114514.demo.pojo.StudentQuestionnaireTeacher;
import com.t114514.demo.service.QuestionnaireAnswerService;
import com.t114514.demo.service.QuestionnaireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/questionnaire/answer")
public class QuestionnaireAnswerController {
    @Autowired
    private QuestionnaireAnswerService questionnaireAnswerService;
    @Autowired
    private QuestionnaireService questionnaireService;

    @PostMapping("/start")
    public Result startAnswer(@RequestBody StudentQuestionnaireTeacher studentQuestionnaireTeacher){
        questionnaireAnswerService.add(studentQuestionnaireTeacher);
        return Result.success();
    }

    @PostMapping("/answerQuestion")
    public Result answerQuestion(@RequestBody Answer newanswer){
        Answer oldanswer=questionnaireAnswerService.isExist(newanswer);
        if(oldanswer==null){
            questionnaireAnswerService.addAnswer(newanswer);
        }
        else {
            questionnaireAnswerService.updateAnswer(newanswer);
        }
        return Result.success();
    }


    @PostMapping("/finish")
    public Result finish(@RequestBody StudentQuestionnaireTeacher studentQuestionnaireTeacher){

        if(questionnaireAnswerService.answerAllQuestion(studentQuestionnaireTeacher)){
            questionnaireAnswerService.finishAnswer(studentQuestionnaireTeacher);
            return Result.success();
        }
        return Result.error("没有回答所有必要的问题");
    }





}
